package com.wl.service;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wl.repository.AccountRepo;
import com.wl.vo.Account;

/**
 * 
 * @author wl
 *
 */
@Service
public class AccountService {
	
	@Autowired
	private AccountRepo accountRepo;
	
	@Autowired
	private EntityManager entityManager;
	
	public Account checkAccount(Account account) {
		//sql查询
//		String sql = "select * from account where user_name = :userName and password = :password";
//		Query sqlQuery = entityManager.createNativeQuery(sql, Account.class);
		
		//hql查询
		String hql = "select a from Account a where a.userName = :userName and a.password = :password";
		Query query = entityManager.createQuery(hql, Account.class);
		
		query.setParameter("userName", account.getUserName());
		query.setParameter("password", account.getPassword());
		try {
			account = (Account) query.getSingleResult();
			System.out.println("=========================账号id:"+account.getId());
		} catch (Exception e) {
			account = null;
		}
		return account;
	}
}
